package com.mindmax.user.service.impl;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;

import com.google.common.collect.Lists;
import com.mindmax.user.domain.InviteCode;
import com.mindmax.user.domain.InviteCodeResult;
import com.mindmax.user.mapper.InviteCodeMapper;
import com.mindmax.user.service.IInviteCodeService;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * InviteCodeService业务层处理
 * 
 * @author cab
 * @date 2024-04-22
 */
@Service
public class InviteCodeServiceImpl implements IInviteCodeService
{
    @Autowired
    private InviteCodeMapper inviteCodeMapper;

    /**
     * 查询InviteCode
     * 
     * @param id InviteCode主键
     * @return InviteCode
     */
    @Override
    public InviteCode selectInviteCodeById(Long id)
    {
        return inviteCodeMapper.selectInviteCodeById(id);
    }

    /**
     * 查询InviteCode列表
     * 
     * @param inviteCode InviteCode
     * @return InviteCode
     */
    @Override
    public List<InviteCode> selectInviteCodeList(InviteCode inviteCode)
    {
        return inviteCodeMapper.selectInviteCodeList(inviteCode);
    }

    /**
     * 新增InviteCode
     * 
     * @param inviteCode InviteCode
     * @return 结果
     */
    @Override
    public int insertInviteCode(InviteCode inviteCode)
    {
        inviteCode.setCreateTime(DateUtils.getNowDate());
        return inviteCodeMapper.insertInviteCode(inviteCode);
    }

    /**
     * 修改InviteCode
     * 
     * @param inviteCode InviteCode
     * @return 结果
     */
    @Override
    public int updateInviteCode(InviteCode inviteCode)
    {
        return inviteCodeMapper.updateInviteCode(inviteCode);
    }

    /**
     * 批量删除InviteCode
     * 
     * @param ids 需要删除的InviteCode主键
     * @return 结果
     */
    @Override
    public int deleteInviteCodeByIds(Long[] ids)
    {
        return inviteCodeMapper.deleteInviteCodeByIds(ids);
    }

    /**
     * 删除InviteCode信息
     * 
     * @param id InviteCode主键
     * @return 结果
     */
    @Override
    public int deleteInviteCodeById(Long id)
    {
        return inviteCodeMapper.deleteInviteCodeById(id);
    }

    @Override
    public String gen(Long userId, String tenantId) {
        ZoneId        targetZone    = ZoneId.of("Asia/Shanghai");

        LocalDateTime now     = LocalDateTime.now();
        ZonedDateTime zonedDateNow = now.atZone(targetZone);
        Date myDateNow = Date.from(zonedDateNow.toInstant());
        String code = RandomStringUtils.randomAlphanumeric(8).toUpperCase();
        InviteCode inviteCode = new InviteCode();
        inviteCode.setTenantId(Long.valueOf(tenantId));
        inviteCode.setInviteCode(code);
        LocalDateTime localDateTime = now.plusDays(7);
        // 假设 "Asia/Shanghai" 是目标时区
        ZonedDateTime zonedDateTime = localDateTime.atZone(targetZone);
        Date          expiredDate          = Date.from(zonedDateTime.toInstant());
        inviteCode.setExpireTime(expiredDate);
        inviteCode.setCreateUser(userId);
        inviteCode.setCreateTime(myDateNow);
        inviteCodeMapper.deleteInviteCodeByInviteCode(tenantId);
        inviteCodeMapper.insertInviteCode(inviteCode);
        return code;
    }

    @Override
    public List<InviteCodeResult> selectInviteCodeByTenantIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Lists.newArrayList();
        }
        return inviteCodeMapper.selectInviteCodeByTenantIds(ids);
    }
}
